package com.ht.medicaltreatment.view

import android.content.Context
import android.graphics.*
import androidx.core.content.ContextCompat
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.WeekView
import com.ht.medicaltreatment.R
import com.xgst.lib.utils.ScreenUtils

class MainWeekView(context: Context) : WeekView(context) {
    //选中背景画笔
    private val mBgPaint = Paint()

    //文本画笔
    private val mTextPaint = Paint()

    private var mRadius = 0

    init {
        mTextPaint.textSize = ScreenUtils.dp2px(context,14).toFloat()
        mTextPaint.setColor(Color.BLACK)
        mTextPaint.isAntiAlias = true
        mTextPaint.isFakeBoldText = true

        mBgPaint.isAntiAlias = true
        mBgPaint.style = Paint.Style.FILL
        mBgPaint.color = Color.WHITE
        mBgPaint.textAlign = Paint.Align.CENTER
    }

    override fun onPreviewHook() {

        mRadius = Math.min(mItemWidth,mItemHeight) / 11 *5
    }

    //动画效果
    override fun onChangeItemTo(from: Int, to: Int) {

    }

    /**
     * 绘制选中的日子
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return 返回true 则绘制onDrawScheme，因为这里背景色不是是互斥的，所以返回true
     */
    override fun onDrawSelected(
        canvas: Canvas?,
        calendar: Calendar?,
        x: Int,
        hasScheme: Boolean
    ): Boolean {

        val left = x + mItemWidth/3 - 12
        val right = left + mItemWidth/3 + 24

        val top = y
        val bottom = top + mItemHeight

        val rect = RectF(left.toFloat(),top, right.toFloat(),bottom)

        canvas?.drawRoundRect(rect,5f,5f,mBgPaint)

        return false
    }

    /**
     * 绘制标记的事件日子
     *
     * @param canvas   canvas
     * @param calendar 日历calendar
     * @param x        日历Card x起点坐标
     * @param y        日历Card y起点坐标
     */
    override fun onDrawScheme(canvas: Canvas?, calendar: Calendar?, x: Int) {

    }

    /**
     * 绘制文本
     *
     * @param canvas     canvas
     * @param calendar   日历calendar
     * @param x          日历Card x起点坐标
     * @param y          日历Card y起点坐标
     * @param hasScheme  是否是标记的日期
     * @param isSelected 是否选中
     */
    override fun onDrawText(
        canvas: Canvas?,
        calendar: Calendar?,
        x: Int,
        hasScheme: Boolean,
        isSelected: Boolean
    ) {
        val cx = x + mItemWidth / 3
        val top = y - mItemHeight / 6

        if(isSelected){
            mTextPaint.setColor(ContextCompat.getColor(context,R.color.blue))
            canvas!!.drawText("${calendar!!.day}", cx.toFloat(),mTextBaseLine + top + 10,mTextPaint)
        }else{
            mTextPaint.setColor(ContextCompat.getColor(context,R.color.white))
            canvas!!.drawText("${calendar!!.day}", cx.toFloat(),mTextBaseLine + top + 10,mTextPaint)
        }
    }
}